#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. /minioslib || exit 1

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

if [ -f $SCRIPT_DIR/$PACKAGE_VARIANT.list ]; then
    case $DISTRIBUTION in
    "jammy" | "noble")
        install_packages --add-packages "libatk1.0-data" --replace-packages "libjpeg62-turbo:libjpeg-turbo8 librest-1.0-0:librest-0.7-0 libtiff6:libtiff5"
        ;;
    "bookworm" | "trixie" | "testing" | "kali-rolling")
        install_packages
        ;;
    "sid")
        install_packages
        ;;
    *)
        install_packages
        ;;
    esac
fi


if [ -f /usr/share/applications/yad-icon-browser.desktop ]; then
    rm -f /usr/share/applications/yad-icon-browser.desktop
fi

if [ ! -L /usr/bin/gdk-pixbuf-query-loaders ]; then
    ln -s /usr/lib/x86_64-linux-gnu/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders /usr/bin/gdk-pixbuf-query-loaders
fi

cat <<EOF >/usr/lib/systemd/system/xorg.service
[Unit]
Description=X-Window
ConditionKernelCommandLine=!text
After=systemd-user-sessions.service

[Service]
ExecStart=/bin/su --login -c "/usr/bin/startx -- :0 vt7 -ac -nolisten tcp" $USER_NAME

EOF

if [ -d /etc/systemd/system ]; then
    mkdir -p /etc/systemd/system
fi
if [ ! -L /etc/systemd/system/display-manager.service ]; then
    ln -s /usr/lib/systemd/system/xorg.service /etc/systemd/system/display-manager.service
fi
if [ -L /etc/systemd/system/display-manager.service ]; then
    echo "Display manager link exists."
else
    exit 1
fi
